package com.jht.bletool2java.activity.setting;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.view.View;
import android.widget.Toast;

import com.jht.bletool2java.BLEApplication;
import com.jht.bletool2java.BuildConfig;
import com.jht.bletool2java.R;
import com.jht.bletool2java.activity.setting.bean.SettingItem;
import com.jht.bletool2java.config.BLEConfiguration;

import java.util.ArrayList;
import java.util.List;

public class SettingPresenter implements ISetting.Presenter {
    private static final String TAG = "SettingPresenter";
    private ISetting.UI settingActivity;

    public SettingPresenter(ISetting.UI settingActivity) {
        this.settingActivity = settingActivity;
    }

    @Override
    public List<SettingItem> getSettingItemData(List<View.OnClickListener> listenerArrayList) {
        Resources resources = BLEApplication.getBLEApplicationContext().getResources();

        List<SettingItem> settingItemList = new ArrayList<>();
        //item1
        String setScanTime = resources.getString(R.string.set_scan_time);
        String currentScanTime = resources.getString(R.string.scan_description)
                + BLEApplication.getBLEConfiguration().getScanTime() + resources.getString(R.string.second);

        SettingItem item1 = new SettingItem(R.drawable.icon_set_time, setScanTime, currentScanTime, listenerArrayList.get(0));
        //item2
        String version = resources.getString(R.string.version);
        String currentVersion = BuildConfig.VERSION_NAME;
        SettingItem item2 = new SettingItem(R.drawable.icon_version, version, currentVersion, listenerArrayList.get(1));
        //item3
        String what_new = resources.getString(R.string.what_is_new);
        String show_change = resources.getString(R.string.show_change);
        SettingItem item3 = new SettingItem(R.drawable.icon_new_change_gray, what_new, show_change, listenerArrayList.get(2));
        //add all items
        settingItemList.add(item1);
        settingItemList.add(item2);
        settingItemList.add(item3);

        return settingItemList;
    }

    @Override
    public boolean changeScanTime(int time) {
        if (time > 120) {
            Toast.makeText(BLEApplication.getBLEApplicationContext(), BLEApplication.getBLEApplicationContext().getResources().getText(R.string.scan_time_limit), Toast.LENGTH_SHORT).show();
            return false;
        } else {
            BLEApplication.getBLEConfiguration().setScanTime(time);
            settingActivity.changeScanTimeText(time);
            SharedPreferences app_config = BLEApplication.getBLEApplicationContext().getSharedPreferences("app_config", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = app_config.edit();
            edit.putInt(BLEConfiguration.KEY_SCAN_TIME, time);
            edit.apply();
            return true;
        }
    }

}
